//
//  MarketTypesCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/5.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "MarketTypesCell.h"
#import "ImageTitleBlock.h"
#import "MMMarketType.h"

static const NSInteger kViewTag = 4000;

@implementation MarketTypesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)contentWithArray:(NSMutableArray *)array {
    if(self.addbg) {
        [self.addbg removeFromSuperview];
        self.addbg = nil;
    }
    if (array.count == 0)
        return;

    self.addbg = [UIView new];
    self.addbg.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.addbg];
    
    [self.addbg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-15);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
    }];
    
    CGFloat blockX = 0, blockY = 0;
    for(NSUInteger i = 0;  i< array.count; i++) {
        blockX = ScreenWidth / 5 * i;
        if (i >= 5) {
            blockY = 80;
            blockX = ScreenWidth / 5 * (i - 5);
        }
        MMMarketType *one = [array objectAtIndex:i];
        ImageTitleBlock *block = [ImageTitleBlock new];
        block.title.text = one.type_name;
        block.backgroundColor = [UIColor whiteColor];
        [block.img sd_setImageWithURL:[NSURL URLWithString:one.img_url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        //block.img.image = [UIImage imageNamed:@"placeholder.png"];
        block.frame = CGRectMake(blockX, blockY, ScreenWidth / 5, 80);
        [self.addbg addSubview:block];
        
        block.tag = kViewTag + i;
        block.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        tap.numberOfTapsRequired = 1;
        [block addGestureRecognizer:tap];
    }
}

- (void)click:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickMarketTypesOne:)]) {
        [self.delegate clickMarketTypesOne:recognizer.view.tag - kViewTag];
    }
}

@end
